<?php
declare (strict_types = 1);

namespace app\admin\model\erp;

use bbweb\traits\ModelTrait;
use bbweb\basic\BaseModel;

/**
 * 客户-供应商分类模型
 */
class VendorsTypes extends BaseModel
{
    /**
     * 数据表主键
     * @var string
     */
    protected $pk = 'id';

    /**
     * 模型名称
     * @var string
     */
    protected $name = 'erp_vendors_types';

    use ModelTrait;

    // 设置字段信息
    protected $schema = [
        'id'	        => 'int',
        'pid'           => 'int',
        'name'          => 'varchar',
        'path'          => 'varchar',
        'visable'       => 'tinyint',
        'update_at'     => 'int'
    ];

    /**
     * * @param int $op 0=列表、1=左侧树形、2=select
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function typeAll($op = 0)
    {
        $typeAll = self::order('id ASC')->select();
        if($op == 2)
            return self::tidySelect($typeAll);
        else if($op == 1)
            return self::tidyLeftTree($typeAll);
        else
            return self::tidyTree($typeAll);
    }

    /**
     * 页面左侧树形展示
     * @param $list
     * @param int $pid
     * @param array $navList
     * @return array
     */
    public static function tidyLeftTree($list)
    {
        $data[] = [
            'title' => '供应商分类',
            'spread' => true,
            'disabled' => false,
            'id' => '0',
        ];
        foreach ($list as $k => $menu) {
            $menu = $menu->getData();
            if ($menu['pid'] == 0) {
                $info = [
                    'title' => $menu['name'],
                    'spread' => true,
                    'id' => $menu['id'],
                ];
                unset($list[$k]);
                foreach ($list as $kb => $mb) {
                    $mb = $mb->getData();
                    if ($mb['pid'] == $menu['id']) {
                        unset($list[$kb]);
                        $info['children'][] = [
                            'title' => $mb['name'],
                            'id' => $mb['id'],
                        ];
                    }
                }
                $data[] = $info;
            }
        }
        return $data;
    }

    /**
     * select选择项
     * @param $list
     * @param int $pid
     * @param array $navList
     * @return array
     */
    public static function tidySelect($list, $pid = 0, $navList = [])
    {
        foreach ($list as $k => $menu) {
            $menu = $menu->getData();
            if ($menu['pid'] == $pid) {
                unset($list[$k]);
                $menu['children'] = self::tidySelect($list, $menu['id']);
                $navList[] = $menu;
            }
        }
        return $navList;
    }

    /**
     * 列表页面展示
     * @param $list
     * @param int $pid
     * @param array $navList
     * @return array
     */
    public static function tidyTree($list, $pid = 0, $navList = [])
    {
        foreach ($list as $k => $menu) {
            $menu = $menu->getData();
            if ($menu['pid'] == $pid) {
                unset($list[$k]);
                $navList[] = $menu;
                $navList = self::tidyTree($list, $menu['id'],$navList);
            }
        }
        return $navList;
    }

    /*
     * 子菜单列表
     */
    public static function setPath() {
        $result = self::order('id ASC')->select()->toArray();
        if(is_array($result)) {
            foreach($result as $r) {
                $path = self::getPath($r['id'],$result);
                self::where('id',$r['id'])->update(['path'=>$path]);
            }
        }
        return true;
    }

    /*
     * 获取path
     */
    protected static function getPath($bid, $binfo) {
        $path = $bid;
        if(is_array($binfo)) {
            foreach($binfo as $bdata) {
                if($bdata['pid'] && $bdata['id'] != $bid && $bdata['pid']== $bid) {
                    $path .= ','. self::getPath($bdata['id'],$binfo);
                }
            }
        }
        return $path;
    }

    /**
     * @param $id
     * @return bool
     */
    public static function delMenu($id)
    {
        if (self::where('pid', $id)->count())
            return self::setErrorInfo('请先删除该菜单下的子菜单!');
        return self::del($id);
    }
}
